package com.wordsay.analyer.impl;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lionsoul.jcseg.core.ISegment;
import org.lionsoul.jcseg.core.IWord;
import org.lionsoul.jcseg.core.JcsegTaskConfig;
import org.lionsoul.jcseg.core.SegmentFactory;

import com.wordsay.analyer.Analyer;
import com.wordsay.context.Context;
import com.wordsay.core.Wordsay;

/**
 * 分词分析器（使用jcseg进行分词）
 * @author squirrelrao
 *
 */
public class JcsegAnalyer implements Analyer {
	
	private static Logger log = LogManager.getLogger(JcsegAnalyer.class.getName());  


	@Override
	public Object execute(Object target) throws Exception {
		
		Context context = Context.getInstance();
		ISegment seg = SegmentFactory.createJcseg(JcsegTaskConfig.COMPLEX_MODE, new Object[]{context.getJcseConfig(),context.getDic()});
		
		//对目标字符串进行分词
		String  str = ((StringBuffer)target).toString();
		seg.reset(new StringReader(str));
		
		List<IWord> iwords = new ArrayList<IWord>();
		IWord word = null;
		String isFilterStopuseWords = context.getContextMap().get("isFilterStopuseWords");
		while((word = seg.next()) != null){
			
			if("".equals(word.getValue().trim())){
				log.info("---过滤掉停用词---> (空格)"+word.getValue());
				System.out.println("---过滤掉停用词---> (空格)"+word.getValue());
				continue;
				
			} else if(isFilterStopuseWords.equalsIgnoreCase("true") && context.getStopWordsMap().containsKey(word.getValue().trim())){
				log.info("---过滤掉停用词---> "+word.getValue().trim());
				System.out.println("---过滤掉停用词---> "+word.getValue().trim());
				continue;
			}else{
				iwords.add(word);

			}
		}
	
		return iwords;
	}

}
